package org.jeecg.modules.wmhb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.common.util.IpUtils;
import org.jeecg.common.util.SpringContextUtils;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.system.entity.SysRolePermission;
import org.jeecg.modules.wmhb.entity.WmContractDep;
import org.jeecg.modules.wmhb.mapper.WmContractDepMapper;
import org.jeecg.modules.wmhb.service.IWmContractDepService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @Description: 合同产品与事业部映射
 * @Author: jeecg-boot
 * @Date:   2024-09-05
 * @Version: V1.0
 */
@Service
public class WmContractDepServiceImpl extends ServiceImpl<WmContractDepMapper, WmContractDep> implements IWmContractDepService {
    @Override
    public void saveBuItem(String wmContractId, String buId, String wmContractProductIds) {
        LambdaQueryWrapper<WmContractDep> query = new QueryWrapper<WmContractDep>().lambda().eq(WmContractDep::getBuId, buId);
        this.remove(query);
        List<WmContractDep> list = new ArrayList<WmContractDep>();
        String[] arr = wmContractProductIds.split(",");
        for (String p : arr) {
            if(oConvertUtils.isNotEmpty(p)) {
                WmContractDep wmContractDep = new WmContractDep(wmContractId, buId, p);
                list.add(wmContractDep);
            }
        }
        this.saveBatch(list);
    }

    @Override
    public void saveBuItem(String wmContractId, String buId, String wmContractProductIds, String lastWmContractProductIds) {
        List<String> add = getDiff(lastWmContractProductIds, wmContractProductIds);
        if(add != null && add.size()>0) {
            List<WmContractDep> list = new ArrayList<WmContractDep>();
            for (String p : add) {
                if(oConvertUtils.isNotEmpty(p)) {
                    WmContractDep wmContractDep = new WmContractDep(wmContractId, buId, p);
                    list.add(wmContractDep);
                }
            }
            this.saveBatch(list);
        }

        List<String> delete = getDiff(wmContractProductIds, lastWmContractProductIds);
        if(delete != null && delete.size() > 0) {
            for (String wmItemId : delete) {
                this.remove(new QueryWrapper<WmContractDep>().lambda().eq(WmContractDep::getBuId, buId).eq(WmContractDep::getWmContractProductId, wmItemId));
            }
        }
    }

    /**
     * 从diff中找出main中没有的元素
     * @param main
     * @param diff
     * @return
     */
    private List<String> getDiff(String main,String diff){
        if(oConvertUtils.isEmpty(diff)) {
            return null;
        }
        if(oConvertUtils.isEmpty(main)) {
            return Arrays.asList(diff.split(","));
        }

        String[] mainArr = main.split(",");
        String[] diffArr = diff.split(",");
        Map<String, Integer> map = new HashMap(5);
        for (String string : mainArr) {
            map.put(string, 1);
        }
        List<String> res = new ArrayList<String>();
        for (String key : diffArr) {
            if(oConvertUtils.isNotEmpty(key) && !map.containsKey(key)) {
                res.add(key);
            }
        }
        return res;
    }
}
